package com.lam.code.utils;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lam
 * @description 分页辅助类
 * @date 2019/10/8 20:26
 */
public final class PageUtil {
    private PageUtil(){}

    /** 分页查询参数封装 */
    @SuppressWarnings({"unchecked"})
    public static Page getPage(Page page,Map<String, Object> params) {
        Integer current = 1;
        Integer size = 10;
        String orderBy = "id_", sortAsc = null, openSort = "Y";
        String asc = (String)params.get("asc");
        String desc = (String)params.get("desc");
        if (ObjectUtil.isNotEmpty(params.get("pageNumber"))) {
            current = Integer.valueOf(params.get("pageNumber").toString());
        }
        if (ObjectUtil.isNotEmpty(params.get("pageIndex"))) {
            current = Integer.valueOf(params.get("pageIndex").toString());
        }
        if (ObjectUtil.isNotEmpty(params.get("pageSize"))) {
            size = Integer.valueOf(params.get("pageSize").toString());
        }
        if (ObjectUtil.isNotEmpty(params.get("limit"))) {
            size = Integer.valueOf(params.get("limit").toString());
        }
        if (ObjectUtil.isNotEmpty(params.get("offset"))) {
            current = Integer.valueOf(params.get("offset").toString()) / size + 1;
        }
        if (ObjectUtil.isNotEmpty(params.get("sort"))) {
            orderBy = (String)params.get("sort");
            params.remove("sort");
        }
        if (ObjectUtil.isNotEmpty(params.get("orderBy"))) {
            orderBy = (String)params.get("orderBy");
            params.remove("orderBy");
        }
        if (ObjectUtil.isNotEmpty(params.get("sortAsc"))) {
            sortAsc = (String)params.get("sortAsc");
            params.remove("sortAsc");
        }
        if (ObjectUtil.isNotEmpty(params.get("openSort"))) {
            openSort = (String)params.get("openSort");
            params.remove("openSort");
        }
        Object filter = params.get("filter");
        if (filter != null) {
            params.putAll(JSON.parseObject(filter.toString(), Map.class));
        }
        if (size == -1) {
            setOrderField(orderBy, sortAsc, openSort, asc, desc, page);
            return page;
        }
        page.setCurrent(current);
        page.setSize(size);
        setOrderField(orderBy, sortAsc, openSort, asc, desc, page);
        return page;
    }

    private static void setOrderField(String orderBy, String sortAsc, String openSort, String asc, String desc, Page<Long> page) {
        if ("Y".equals(openSort)) {
            if (ObjectUtil.isEmpty(asc) && DataUtil.isEmail(desc)) {
                String[] split = orderBy.split(",");
                List<OrderItem> orderItemList = new ArrayList<>();
                for (String order : split) {
                    orderItemList.add("Y".equals(sortAsc) ? OrderItem.asc(order) : OrderItem.desc(order));
                }
                page.setOrders(orderItemList);
            } else {
                if (ObjectUtil.isNotEmpty(asc)) {
                    String[] split = asc.split(",");
                    List<OrderItem> orderItemList = new ArrayList<>();
                    for (String order : split) {
                        orderItemList.add(OrderItem.asc(order));
                    }
                    page.setOrders(orderItemList);
                }
                if (ObjectUtil.isNotEmpty(desc)) {
                    String[] split = desc.split(",");
                    List<OrderItem> orderItemList = new ArrayList<>();
                    for (String order : split) {
                        orderItemList.add(OrderItem.desc(order));
                    }
                    page.setOrders(orderItemList);
                }
            }
        }
    }



}
